package com.oldroid.apps.getit.ui;

import java.text.DecimalFormat;

import org.achartengine.chart.PieChart;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.oldroid.apps.getit.R;

public class StatusDetailFragment extends Fragment {
	public static final String TAG = "StatusDetailFragment";
	private static final double GIGA_BYTES =  Math.pow(2, 30); // there are 2^30 bytes in 1 Gigabyte
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

//	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_status_detail, null);		
		TextView details = (TextView) root.getChildAt(1);
		
		MemoryInfo memInfo = new ActivityManager.MemoryInfo();
		ActivityManager mgr = (ActivityManager) HomeActivity.mContext.getSystemService(Context.ACTIVITY_SERVICE); 
        mgr.getMemoryInfo(memInfo);
        String s = String.valueOf(memInfo.availMem / Math.pow(10, 6)); 
		
		//details.setText("Available memory: " + s + " MegaBytes");
		details.setText("Total number of downloads: " + HomeActivity.totalCount);
		
		
		 //this part gives you more system memory information
		 //http://stackoverflow.com/questions/4799643/getting-all-the-total-and-available-space-on-android
		
		 /**
		  * Get the available storage on the SD Card as that's where our
		  * downloads default to being saved.
		  * 
		  * Block sizes are in bytes
		  */
		 StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
		 long blockSize = statFs.getBlockSize();
		 long totalSize = statFs.getBlockCount() * blockSize;
		 long availableSize = statFs.getAvailableBlocks() * blockSize; // only returns those blocks that are usable by applications
		 long freeSize = statFs.getFreeBlocks() * blockSize; // includes every free block, including unusuable reserved blocks
		 
		 
		 double availableSizeInGb, totalSizeInGb, usedInGb;
		 DecimalFormat df = new DecimalFormat("#.##"); // round to two decimal places
		 availableSizeInGb = Double.parseDouble(df.format(availableSize / GIGA_BYTES));
		 totalSizeInGb = Double.parseDouble(df.format(totalSize / GIGA_BYTES));
		 usedInGb = totalSizeInGb - availableSizeInGb;
		 
		 
		 
		 details.append("\n blockSize = " + blockSize);
		 details.append("\nTotal Memory: " + totalSizeInGb+ " GB \nAvailable Memory: " + availableSizeInGb + " GB");

	
		 
		  
		 CategorySeries memory = new CategorySeries("System Memory");
		 memory.add("Available Memory", availableSize);
		 memory.add("Occupied Memory", totalSize - availableSize);
		 
		 DefaultRenderer renderer = new DefaultRenderer();
		 
		 PieChart pieChart = new PieChart(memory, renderer);
		 
		 ImageView image = (ImageView) root.findViewById(R.id.canvas);

	     
		 
		 Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
		 Canvas c = new Canvas(b);
		 Paint paint = new Paint();
		 
		 //pieChart.draw(c, 20, 20, 30, 30, paint);
		 c.drawCircle(60, 50, 50, paint);
		 
		 image.setImageBitmap(b);
		 
	
		root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
		return root;
	}
//	}
//	
	
}
